﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DBO_ShopMob;
using WP_Customer_ShopMob.Helper;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace WP_Customer_ShopMob.Model
{
    /// <summary>
    /// Repository utilisant le webservice
    /// </summary>
    class Repository : IRepository
    {

        /// <summary>
        /// Ip du serveur avec le port
        /// </summary>
        private string ipServer = "10.10.162.60:8080";

        /// <summary>
        /// Dictionnaire contenant la liste des urls disponible
        /// </summary>
        private Dictionary<string, string> urls;

        /// <summary>
        /// Helpers pour appelé le webservice
        /// </summary>
        private WebServiceHelpers wsh;

        /// <summary>
        /// Constructeur standard
        /// </summary>
        public Repository() {
            //on instancie l'hêlpers et on renseingne le dictionnaire
            wsh = new WebServiceHelpers();
            urls = new Dictionary<string, string>();

            urls.Add("addClient", String.Format("http://{0}/back-office/shopmob/addclient", ipServer));
            urls.Add("getClientParID", String.Format("http://{0}/back-office/shopmob/client?id=", ipServer));
            urls.Add("connexion", String.Format("http://{0}/back-office/shopmob/connexion", ipServer));
            urls.Add("getCategorieMere",String.Format("http://{0}/back-office/shopmob/categoriesmere", ipServer));
            urls.Add("getCategorieFille",String.Format("http://{0}/back-office/shopmob/categoriesfille?id=", ipServer));
            urls.Add("getCategorieArticlesByCateg", String.Format("http://{0}/back-office/shopmob/articlesbycat?id=", ipServer));
            urls.Add("getArticlesByNom", String.Format("http://{0}/back-office/shopmob/articlesbynom?nom=", ipServer));
            urls.Add("getMagasinsByArticle", String.Format("http://{0}/back-office/shopmob/getMagasinByArticle?id=", ipServer));
            urls.Add("getArticlesById", String.Format("http://{0}/back-office/shopmob/articlebyid?id=", ipServer));            
        }


        /// <summary>
        /// Voir IRepository
        /// </summary>
        public void AjouterClient(Client client, ResultHandler methodeToExec)
        {
            wsh.CallWebService(urls["addClient"], client, true, methodeToExec);
        }

        /// <summary>
        /// Voir IRepository
        /// </summary>
        public void Connexion(string login, string motdePasse, ResultHandler methodeToExec)
        {
            //on met l'email et le mot de passe sous forme JSON
            //string toSend = "[{\"email\":\"" + login + "\", \"motDePasse\":\"" + motdePasse + "\"}]";
            //on envoie au webService les infos
            //wsh.CallWebService(urls["connexion"], toSend, false, methodeToExec);
            new DebugRepository().Connexion(login, motdePasse, methodeToExec);
        }

        /// <summary>
        /// Voir IRepository
        /// </summary>
        public void GetClientParID(string login, ResultHandler methodeToExec)
        {
            //on indique le login et on envoi le tout au webservice
            //wsh.CallWebService(urls["getClientParID"] + login, methodeToExec);
            new DebugRepository().GetClientParID(login, methodeToExec);
        }


        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetCategoriesMere(ResultHandler methodToExec)
        {
            wsh.CallWebService(urls["getCategorieMere"], methodToExec);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetCategoriesFille(long id, ResultHandler methodToExec)
        {
            wsh.CallWebService(urls["getCategorieFille"] + id, methodToExec);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetArticlesByCateg(long id, ResultHandler methodeToExec)
        {
            wsh.CallWebService(urls["getCategorieArticlesByCateg"] + id, methodeToExec);
        }

        /// <summary>
        // Voir IRepository
        /// </summary>
        public void GetMagasinForArticle(long id, ResultHandler methodToExec)
        {
            wsh.CallWebService(urls["getMagasinsByArticle"] + id, methodToExec);
        }


        public void GetArticlesByNom(string nom, ResultHandler methodToExec)
        {
            wsh.CallWebService(urls["getArticlesByNom"] + nom, methodToExec);
        }

        public void GetArticlesById(long idArticle, ResultHandler methodToExec)
        {
            wsh.CallWebService(urls["getArticlesById"] + idArticle, methodToExec);
        }
    }
}
